package moddlewere

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"week_exam_2_3/pkg"
)

func OnLine(context *gin.Context) {
	token := context.Request.Header.Get("token")
	if token == "" {
		context.AbortWithStatusJSON(
			http.StatusBadRequest,
			gin.H{
				"code":    http.StatusBadRequest,
				"message": "未登录",
			})
		return
	}
	getToken, err := pkg.GetToken(token)
	if err != nil {
		context.AbortWithStatusJSON(
			http.StatusOK,
			gin.H{
				"code":    http.StatusBadRequest,
				"message": fmt.Sprintf("err:%s", err.Error()),
			})
		return
	}
	user_str := getToken["user"].(string)
	context.Request.ParseForm()
	context.Request.Form.Set("user", user_str)
	fmt.Println("user_json:", user_str)
	context.Next()
}
